/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hiddendanger.controller;

import com.google.gson.Gson;
import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.DeptBean;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.common.web.Util;
import com.koron.hiddendanger.bean.CheckRecordAtrrBean;
import com.koron.hiddendanger.bean.CheckRecordReport;
import com.koron.hiddendanger.domain.CheckRecord;
import com.koron.hiddendanger.domain.ChecklistEntryReference;
import com.koron.hiddendanger.domain.ItemCheckRecord;
import com.koron.hiddendanger.service.api.CheckRecordService;
import com.koron.hiddendanger.service.api.ChecklistEntryReferenceService;
import com.koron.hiddendanger.uitls.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "checkRecord", description = "检查记录相关接口")
@RestController
@RequestMapping("/checkRecord")
public class CheckRecordController {

    @Autowired
    private CheckRecordService recordService;

    private final static  Logger logger = LoggerFactory.getLogger(CheckRecordController.class);

    @Autowired
    private Util util;

    @Autowired
    private ChecklistEntryReferenceService referenceService;

    @ApiOperation("保存检查记录接口")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "userCode",value = "用户登录code,pc端不传，移动端必传",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "id", value = "检查记录id,新增不传，修改必传",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planId", value = "检查计划id",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "检查范围——水司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gridCode", value = "检查范围——网格code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gridName", value = "检查范围——网格名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "检查记录名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "计划描述",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "checkTime", value = "检查时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "innerWorkers", value = "内部检查人员code和所在单位code,,格式为staffCode_deptCode,staffCode_deptCode", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "innerWorkersName", value = "内部检查人员名称和所在单位名称,,格式为staffCode_deptCode,staffCode_deptCode", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "outterWorkers", value = "外部检查人员和所在单位,可以输入多个检查人员，不同检查人员之间用,分开，格式：名称_部门，名称_部门", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "identifier", value = "验证人code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "identifierName", value = "验证人名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "manager", value = "负责人code,最终负责人", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "managerName", value = "负责人名称,最终负责人", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "checkLevel", value = "检查层级", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "checkLevelName", value = "检查层级名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "checkTheme", value = "检查主题",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inspectorsSignFile", value = "检查人员签名附件",  dataType = "file", paramType = "query"),
            @ApiImplicitParam(name = "signFile", value = "受检单位负责人签名附件",  dataType = "file", paramType = "query"),
    })
    @RequestMapping(value = "/saveCheckRecord.htm", method = RequestMethod.POST)
    public MessageBean<String> saveCheckRecord(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore CheckRecord checkRecord,
                                               @RequestParam final MultipartFile[] inspectorsSignFile, String keepInspectorsSignFile, String delInspectorsSignFile,
                                               @RequestParam final MultipartFile[] signFile, String keepSignFile, String delSignFile
            /* ,@RequestParam(required = false) final String userCode*/) {
        MessageBean<String> msg = new MessageBean<>();
//        List<StaffBean> staffBeanList= (List<StaffBean>) util.staff(userCode,null).getData();
//        if (null==user){
//            if (null!=staffBeanList&&staffBeanList.size()>0)
//                user=staffBeanList.get(0);
//        }
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        checkRecord.setUpdateTime(new Date());
        checkRecord.setUpdateUser(user.getName());
        checkRecord.setSubmitCode(user.getCode());
        if (StringUtils.isNotEmpty(checkRecord.getInnerWorkers())) {
            String[] codeArray = checkRecord.getInnerWorkers().split("[,，]");
            List<String> codes = Arrays.asList(codeArray).stream().map(String::trim).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            checkRecord.setInnerWorkers(new Gson().toJson(codes));
        }
        if (StringUtils.isNotEmpty(checkRecord.getInnerWorkersName())) {
            String[] namesArray = checkRecord.getInnerWorkersName().split("[,，]");
            List<String> names = Arrays.asList(namesArray).stream().map(String::trim).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            checkRecord.setInnerWorkersName(new Gson().toJson(names));
        }
        if (StringUtils.isNotEmpty(checkRecord.getInspectedUnitLead())) {
            String[] codeArray = checkRecord.getInspectedUnitLead().split("[,，]");
            List<String> codes = Arrays.asList(codeArray).stream().map(String::trim).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            checkRecord.setInspectedUnitLead(new Gson().toJson(codes));
        }
        if (StringUtils.isNotEmpty(checkRecord.getInspectedUnitLeadName())) {
            String[] namesArray = checkRecord.getInspectedUnitLeadName().split("[,，]");
            List<String> names = Arrays.asList(namesArray).stream().map(String::trim).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            checkRecord.setInspectedUnitLeadName(new Gson().toJson(names));
        }
        if (StringUtils.isNotEmpty(checkRecord.getOutterWorkers()) && ("null").equals(checkRecord.getOutterWorkers())) {
            checkRecord.setOutterWorkers(null);
        }
        return recordService.save(user,checkRecord, inspectorsSignFile, keepInspectorsSignFile, delInspectorsSignFile, signFile, keepSignFile, delSignFile);
    }

    @ApiOperation("提交检查记录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jobsTasksId", value = "任务id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/commitCheckRecord.htm", method = RequestMethod.POST)
    public MessageBean<Integer> commitCheckRecord(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String id, @RequestParam(required = false) final String jobsTasksId) {
        if (null == user)
            return MessageBean.create(BusinessCode.NOT_LONGIN.getCode(), BusinessCode.NOT_LONGIN.getDescription(), Integer.class);
        return recordService.commit(user, id, jobsTasksId);
    }

    @ApiOperation("根据主键查询检查记录详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/checkRecordDetails.htm", method = RequestMethod.POST)
    public MessageBean<CheckRecord> checkRecordDetails(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String id) {
        return recordService.queryOne(user, id);
    }

    @ApiOperation("根据主键删除检查记录接口,可批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "检查记录id,多个之间用,分开", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/batchDeleteCheckRecord.htm", method = RequestMethod.POST)
    public MessageBean<Integer> batchDeleteCheckRecord(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@RequestParam final String ids) {
        return recordService.batchDelete(user,ids == null ? null : Arrays.asList(ids.split("[,，]")));
    }

    @ApiOperation("根据指定条件查询检查记录列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "当前登录人code,pc端不传，移动端必传",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planId", value = "检查计划id",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gridCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planType", value = "计划类型",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "submit", value = "检查状态",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "manager", value = "负责人code,最终负责人",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键词",  dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/listCheckRecord.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<CheckRecord>> listCheckRecord(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String userCode, @RequestParam(required = false) final String planId,
                                                              @RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String gridCode, @RequestParam(required = false) final String manager, Integer planType,
                                                              @RequestParam(required = false) final Integer submit, @RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) final String keyword) {
        MessageBean<DataBean<CheckRecord>> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        msg = recordService.queryByCondition(user, userCode, planId, corpCode, gridCode, manager, planType, submit, start, pageSize,keyword);
        if (null != msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }

    @ApiOperation("根据指定了计划下可按指定条件查询检查记录列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "检查计划id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gridCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planType", value = "计划类型",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "submit", value = "检查状态",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "manager", value = "负责人code,最终负责人",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/queryByPlanId.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<CheckRecord>> queryByPlanId(@RequestParam final String planId, @RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String gridCode,
                                                            @RequestParam(required = false) final String manager, @RequestParam(required = false) final Integer planType,
                                                            @RequestParam(required = false) final Integer submit, @RequestParam Integer page, @RequestParam Integer pageSize) {
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        MessageBean<DataBean<CheckRecord>> msg = recordService.queryByPlanId(planId, corpCode, gridCode, manager, planType, submit, start, pageSize);
        if (null != msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }

    @ApiOperation("上传附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "file", value = "要上传的附件", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/saveAttachment.htm", method = RequestMethod.POST)
    public MessageBean<Integer> saveAttachment(@RequestParam final String id, @RequestParam final MultipartFile[] file) {
        return recordService.saveAttachment(id, file);
    }

    @ApiOperation("删除附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fileAddress", value = "要删除的附件地址", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fileName", value = "要删除的附件名称", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/deleteFile.htm", method = RequestMethod.POST)
    public MessageBean<Integer> deleteFile(@RequestParam final String id, @RequestParam final String fileAddress, @RequestParam final String fileName) {
        return recordService.deleteFile(id, fileAddress, fileName);
    }

    @ApiOperation("获取附件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkId", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/fileList.htm", method = RequestMethod.POST)
    public MessageBean<List<CheckRecordAtrrBean>> fileList(@RequestParam final String checkId) {
        return recordService.fileList(checkId);
    }

    @ApiOperation("预览报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/viewReport.htm", method = RequestMethod.POST)
    public MessageBean<CheckRecordReport> viewReport(@RequestParam final String id) {
        return recordService.viewReport(id);
    }

    @ApiOperation("下载预览报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportReport.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportViewReport(@RequestParam final String id) {
        CheckRecordReport checkRecordReport = recordService.viewReport(id).getData();
        Map<String, Object> map = new HashMap<>();
        map.put("data", checkRecordReport);
        File targetFile = new File("static/excel/checkRecordReport.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/checkRecordReport.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("检查记录报告", targetFile, "checkRecordReport.btl", map);
    }


    @ApiOperation("下载预览报告，带图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportViewReport.htm", method = RequestMethod.GET)
    public void exportReport(HttpServletResponse response, @RequestParam(required = false) String id) {
        // 模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/checkRecordReport.xlsx");
        XSSFWorkbook workbook = recordService.exportReport(id, inputStream);
        if (null != workbook) {
            try (OutputStream os = response.getOutputStream()) {
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("检查记录报告.xlsx", StandardCharsets.UTF_8));
                workbook.write(os);
                os.flush();
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
            }
        }
    }

    @ApiOperation("检查明细-保存检查明细")
    @RequestMapping(value = "/saveReferenceItems.htm", method = RequestMethod.POST)
    public MessageBean<Integer> saveReferenceItems(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody ChecklistEntryReference reference) {
        if (null == user)
            return MessageBean.create(BusinessCode.NOT_LONGIN.getCode(), BusinessCode.NOT_LONGIN.getDescription(), Integer.class);
        reference.setUpdateTime(new Date());
        reference.setUpdateUser(user.getCode());
        reference.setUpdateUserName(user.getName());
        return referenceService.save(user, reference);
    }

    @ApiOperation("检查明细-删除检查明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkRecordId", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ids", value = "检查表填写检查记录的id,多个之间用，分开", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/batchDeleteReference.htm", method = RequestMethod.POST)
    public MessageBean<Integer> batchDeleteReference(String checkRecordId, String ids) {
        return referenceService.batchDelete(checkRecordId, (ids == null || "".equals(ids) ? null : Arrays.asList(ids.split("[,，]"))));
    }

    @ApiOperation("检查明细-查看某一条检查明细详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检查表填写检查记录的id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/detailsReference.htm", method = RequestMethod.POST)
    public MessageBean<ChecklistEntryReference> queryOne(String id) {
        return referenceService.queryOne(id);
    }

    @ApiOperation("检查明细-检查表明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkRecordId", value = "检查记录id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/queryReferenceByCheckRecordId.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<ChecklistEntryReference>> queryByCheckRecordId(String checkRecordId, Integer page, Integer pageSize) {
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        MessageBean<DataBean<ChecklistEntryReference>> msg = referenceService.queryByCheckRecordId(checkRecordId, start, pageSize);
        if (null != msg.getData())
            msg.getData().setPageNo(page == null ? 1 : page);
        return msg;
    }

    @ApiOperation("检查明细-检查项记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "referenceId", value = "检查记录使用检查表记录id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/queryItemsByReferenceId.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<ItemCheckRecord>> queryItemsByReferenceId(String referenceId, Integer page, Integer pageSize) {
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        MessageBean<DataBean<ItemCheckRecord>> msg = referenceService.queryItemsByReferenceId(referenceId, start, pageSize);
        if (null != msg.getData())
            msg.getData().setPageNo(page == null ? 1 : page);
        return msg;
    }

    @ApiOperation("PC端首页安全检查记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/indexCheckRecordList.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<CheckRecord>> indexCheckRecordList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                                   @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize) {
        MessageBean<DataBean<CheckRecord>> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        List<DeptBean> deptBeanList = (List<DeptBean>) util.dept(user.getCode()).getData();
        if (deptBeanList == null || deptBeanList.size() <= 0) {
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        String code = user.getCode() + "_" + deptBeanList.get(0).getCode();
        Integer start = null;
        if (page != null) {
            if (page <= 0) {
                page = 1;
                if (pageSize == null || pageSize <= 0) {
                    pageSize = 15;
                }
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
        }
        msg = recordService.indexCheckRecordList(code, start, pageSize);
        if (null != msg.getData())
            msg.getData().setPageNo(page == null ? 1 : page);
        return msg;
    }


    @ApiOperation("检查记录列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "当前登录人code,pc端不传，移动端必传",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planId", value = "检查计划id",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gridCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planType", value = "计划类型",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "submit", value = "检查状态",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "manager", value = "负责人code,最终负责人",  dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String userCode, String planId,
                                    String corpCode, String gridCode, String manager, Integer planType, Integer submit) {
        if (null == user) {
            return null;
        }
        List<CheckRecord> list = recordService.exportList(user, corpCode, userCode, planId, gridCode, manager, planType, submit);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/checkRecordList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/checkRecordList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("检查记录列表", targetFile, "checkRecordList.btl", map);
    }


    @ApiOperation("首页工作台安全检查列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "当前登录人code,pc端不传，移动端必传",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gridCode", value = "检查范围——水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "planType", value = "计划类型",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "submit", value = "检查状态",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "manager", value = "负责人code,最终负责人",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/homePageCheckList.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<CheckRecord>> homePageCheckList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String userCode, @RequestParam(required = false) final String planId,
                                                                @RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String gridCode, @RequestParam(required = false) final String manager, Integer planType,
                                                                @RequestParam(required = false) final Integer submit, @RequestParam Integer page, @RequestParam Integer pageSize) {
        MessageBean<DataBean<CheckRecord>> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        msg = recordService.homePageCheckList(user, userCode, planId, corpCode, gridCode, manager, planType, submit, start, pageSize);
        if (null != msg.getData()) {
            msg.getData().setPageNo(page);
        }
        return msg;
    }

}
